//
//  JobDetailPickupAddressView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/5/30.
//

import UIKit
import RxCocoa
import RxSwift
import SnapKit

class JobDetailPickupAddressView: View {
    private lazy var disposeBag = DisposeBag()
    var pickupAddressLabelLongPressGestureClosure: (()->Void)?
    let titleLabel = ContentLabel(R.font.interRegular(size: 12),hexStr: "#292D3299")
    let backView = CustomBackView()
    //
    let mapNaviBtn = UIButton()
    let pickupAddressLabel = ContentLabel()
    let refNoLabel = ContentLabel(hexStr: "#292D3299")
    override func setUpView() {
        //*******
        titleLabel.text = "PICK-UP ADDRESS"
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(24)
            make.width.height.greaterThanOrEqualTo(0)
        }
        let moreBtn = UIButton()
        self.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel)
            make.right.equalTo(-20)
            make.width.height.equalTo(40)
        }
        //
        self.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(9)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalToSuperview()
        }
        pickupAddressLabel.numberOfLines = 0
        backView.addSubview(pickupAddressLabel)
        pickupAddressLabel.snp.makeConstraints { make in
            make.left.top.equalTo(16)
            make.width.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-16)
        }
        //
        mapNaviBtn.setImage(R.image.jobdetal_pickup_map_navi(), for: UIControl.State.normal)
        backView.addSubview(mapNaviBtn)
        mapNaviBtn.snp.makeConstraints { make in
            make.left.equalTo(pickupAddressLabel.snp.right)
            make.right.equalTo(-8)
            make.width.height.equalTo(40)
            make.centerY.equalTo(pickupAddressLabel)
        }
        //
        moreBtn.tag =  1
        moreBtn.setImage(R.image.more_gray_down_icon(), for: UIControl.State.normal)
        moreBtn.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else {return}
            if moreBtn.tag == 1 {//hide instruction
                moreBtn.tag =  0
                moreBtn.setImage(R.image.more_gray_up_icon(), for: UIControl.State.normal)
                if let _ = strongSelf.backView.superview {
                    strongSelf.backView.removeFromSuperview()
                }
                strongSelf.titleLabel.snp.remakeConstraints { make in
                    make.top.equalTo(20)
                    make.left.equalTo(24)
                    make.width.height.greaterThanOrEqualTo(0)
                    make.bottom.equalToSuperview()
                }
            }else{//show instruction
                moreBtn.tag =  1
                moreBtn.setImage(R.image.more_gray_down_icon(), for: UIControl.State.normal)
                if strongSelf.backView.superview == nil  {
                    strongSelf.addSubview(strongSelf.backView)
                    strongSelf.backView.snp.makeConstraints { make in
                        make.top.equalTo(strongSelf.titleLabel.snp.bottom).offset(9)
                        make.left.equalTo(12)
                        make.right.equalTo(-12)
                        make.height.greaterThanOrEqualTo(0)
                        make.bottom.equalToSuperview()
                    }
                    strongSelf.titleLabel.snp.remakeConstraints { make in
                        make.top.equalTo(20)
                        make.left.equalTo(24)
                        make.width.height.greaterThanOrEqualTo(0)
                    }
                }
            }
        }).disposed(by: disposeBag)
        //
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addressLabelLongPressGesture))
        pickupAddressLabel.addGestureRecognizer(longPressGesture)
        pickupAddressLabel.isUserInteractionEnabled = true
    }
    
    @objc func addressLabelLongPressGesture(){
        self.pickupAddressLabelLongPressGestureClosure?()
    }
    func setData(_ address: String, refNo: String){
        self.pickupAddressLabel.text = address
        if refNo == "" {//不显示refNo
            if let _ = self.refNoLabel.superview {
                self.refNoLabel.removeFromSuperview()
            }
            pickupAddressLabel.snp.remakeConstraints { make in
                make.left.top.equalTo(16)
                make.width.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(-16)
            }
        }else{//显示refNo
            pickupAddressLabel.snp.remakeConstraints { make in
                make.left.top.equalTo(16)
                make.width.height.greaterThanOrEqualTo(0)
            }
            backView.addSubview(refNoLabel)
            refNoLabel.snp.makeConstraints { make in
                make.top.equalTo(pickupAddressLabel.snp.bottom).offset(20)
                make.left.equalTo(pickupAddressLabel)
                make.width.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(-16)
            }
            refNoLabel.text = R.string.localizable.pickUpRef(refNo)
        }
    }
}
